package com.mango.leet.code.week2.game376;

import java.util.Arrays;

/**
 * 100123. 执行操作使频率分数最大
 * https://leetcode.cn/contest/weekly-contest-376/problems/apply-operations-to-maximize-frequency-score/
 * @author mango
 * @since 2023/12/17
 */
public class LC100123 {
    public static int maxFrequencyScore(int[] nums, long k) {
        int result = 0;
        Arrays.sort(nums);
        int[] div = new int[nums.length];
        for (int i = 1; i < nums.length; i++) {
            div[i] = Math.abs(nums[i] - nums[i-1]);
        }
        int sum = div[0];
        for (int i = 0; i < div.length; i++) {
            if (k == 0 && div[i] == 0) {
                result++;
            }
            sum += div[i];
            if (k > 0 && sum > k) {
                return sum-div[i];
            }
        }
        return result;
    }
    public static void main(String[] args) {
        int[] nums = {1,2,6,4};
        int k = 3;
//        int[] nums = {1,4,4,2,4};
//        int k = 0;
        System.out.println(maxFrequencyScore(nums, k));
    }

}
